{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "4b30349e",
   "metadata": {},
   "outputs": [],
   "source": [
    "class Solution:\n",
    "    def findKthLargest(self, nums, k):\n",
    "        def quick_select(nums, k):\n",
    "            # 随机选择基准数\n",
    "            pivot = random.choice(nums)\n",
    "            big, equal, small = [], [], []\n",
    "            # 将大于、小于、等于 pivot 的元素划分至 big, small, equal 中\n",
    "            for num in nums:\n",
    "                if num > pivot:\n",
    "                    big.append(num)\n",
    "                elif num < pivot:\n",
    "                    small.append(num)\n",
    "                else:\n",
    "                    equal.append(num)\n",
    "            if k <= len(big):\n",
    "                # 第 k 大元素在 big 中，递归划分\n",
    "                return quick_select(big, k)\n",
    "            if len(nums) - len(small) < k:\n",
    "                # 第 k 大元素在 small 中，递归划分\n",
    "                return quick_select(small, k - len(nums) + len(small))\n",
    "            # 第 k 大元素在 equal 中，直接返回 pivot\n",
    "            return pivot\n",
    "        \n",
    "        return quick_select(nums, k)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "8bf06999",
   "metadata": {},
   "outputs": [],
   "source": [
    "class Solution:\n",
    "    def findKthLargest(self, nums, k) -> int:\n",
    "        lenth=len(nums)\n",
    "        nums.sort()\n",
    "        return nums[lenth-k]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "5d7bc1d7",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.12"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
